import { open } from '@tauri-apps/plugin-shell'
import { Menu, PredefinedMenuItem } from '@tauri-apps/api/menu'
import { emit } from '@tauri-apps/api/event'
import { exit } from '@tauri-apps/plugin-process'
import util from '../util'
// 退出应用
// await exit(0)
// 重启应用
// await relaunch();

const fontSizes = ['14px', '16px', '18px', '24px', '36px', '48px', '60px', '72px']
const fontFamilies = ['Arial', 'Helvetica', 'Georgia', 'Times New Roman', 'Courier New', 'Lucida Console']
const fontColors = [
  { text: '茶花红', value: '#FF4A58' },
  { text: '芒果橙', value: '#F7880D' },
  { text: '栀子黄', value: '#ebb10D' },
  { text: '青草绿', value: '#6DD400' },
  { text: '碧玉青', value: '#05D892' },
  { text: '宝石蓝', value: '#158AFF' },
  { text: '青莲紫', value: '#8B0DCB' },
]

const countdownConfigs = [
  { text: '取消', value: 0 },
  { text: '15秒', value: 15 },
  { text: '3分钟', value: 180 },
  { text: '5分钟', value: 300 },
  { text: '7分钟', value: 420 },
  { text: '10分钟', value: 600 },
  { text: '12分钟', value: 720 },
  { text: '15分钟', value: 900 },
  { text: '30分钟', value: 1800 },
  { text: '45分钟', value: 2700 },
  { text: '1小时', value: 3600 },
]

async function createMenuSeparator() {
  return await PredefinedMenuItem.new({ item: 'Separator' })
}

/**
 * 创建托盘菜单
 */
async function createMenu() {
  const options = {
    id: 'tray-menu',
    // items 的显示顺序是倒过来的
    items: [
      {
        id: 'font-size',
        text: '字体大小',
        items: util.menu.build(fontSizes, value => emit('change-font-size', { value })),
      },
      {
        id: 'font-color',
        text: '字体颜色',
        items: util.menu.build(fontColors, value => emit('change-font-color', { value })),
      },
      {
        id: 'font-family',
        text: '字体风格',
        items: util.menu.build(fontFamilies, value => emit('change-font-family', { value })),
      },
      // 分割线
      await createMenuSeparator(),
      {
        id: 'countdown',
        text: '倒计时',
        items: util.menu.build(countdownConfigs, value => emit('change-countdown', { value })),
      },
      await createMenuSeparator(),
      {
        id: 'v2-tauri-docs',
        text: '文档',
        action: async () => await open('https://v2.tauri.app/zh-cn/learn/system-tray'),
      },
      { id: 'github', text: 'GitHub', action: async () => await open('https://github.com/tauri-apps/tauri') },
      { id: 'quit', text: '退出', action: () => exit(0) },
    ],
  }
  return await Menu.new(options)
}

export default createMenu
